' Program generates fractal animation that looks like atoms.
' While it uses a simple formula to calculate the color of every pixel,
' visual effect is quite impressive. Formula was accidentally discovered.
'
' This program is free software: released under Creative Commons Zero (CC0) license
' by Svjatoslav Agejenko.
' Email: svjatoslav@svjatoslav.eu
' Homepage: http://www.svjatoslav.eu

' Changelog:
' 2002, Original version
' 2024 - 2025, Improved program readability

SCREEN 13
DIM SHARED pixelByte AS STRING * 1

zoomFactor = 100
frameNumber = 0
fileNameChar1 = 97
fileNameChar2 = 97

1
SOUND 0, 4.5

frameNumber = frameNumber + 1

' Calculate transformed screen dimensions based on current zoom level
transformedScreenWidth = 320 * zoomFactor / 30
transformedScreenHeight = 200 * zoomFactor / 30

' Calculate starting coordinates to center the fractal pattern
startCoordinateX = 160 - (transformedScreenWidth / 2)
startCoordinateY = 100 - (transformedScreenHeight / 2)

' CLS

' Generate fractal pattern pixel by pixel
FOR currentY = 0 TO 199
    FOR currentX = 0 TO 319
        ' Calculate new coordinates with current zoom level
        newX = startCoordinateX + (transformedScreenWidth * currentX / 320)
        newY = startCoordinateY + (transformedScreenHeight * currentY / 200)

        ' Calculate color value using fractal formula
        ' Formula: sin((x² + y²) / 10) * 6 + 23
        colorValue = SIN((newX ^ 2 + newY ^ 2) / 10) * 6 + 23

        ' Clamp color values to valid range (16-31)
        IF colorValue < 16 THEN colorValue = 16
        IF colorValue > 31 THEN colorValue = 31
        PSET (currentX, currentY), colorValue
    NEXT currentX
NEXT currentY


' Decrease zoom factor for next frame to create zooming effect
zoomFactor = zoomFactor / 1.1

' Continue generating frames while zoom factor is above minimum threshold
IF zoomFactor > 5 THEN GOTO 1
